/*************************************************************************
    > File Name: client.c
    > Author: sun
    > Mail: sunhonghe123@qq.com 
    > Created Time: 2024年06月16日 星期日 21时41分45秒
 ************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>

int main()
{
	//创建套接字
	int sock = socket(AF_INET, SOCK_STREAM, 0);
	if(sock == -1)
	{
		perror("socket");
		exit(0);
	}

	struct sockaddr_in addr;
	addr.sin_family = AF_INET;
	addr.sin_port = htons(8080);

	if (inet_pton(AF_INET, "192.168.145.238", &addr.sin_addr) <= 0)
	{
		perror("invalid address");
		exit(0);
	}

	//连接
	int server = connect(sock, (struct sockaddr*)&addr, sizeof(addr));
	if(server == -1)
	{
		perror("Connection Failed");
		exit(0);
	}
	printf("成功连接到了这个服务器...\n");
	
	char* filename = "file.txt";

	//打开文件
	FILE *fp = fopen(filename, "wb");
	if(!fp)
	{
		perror("file open error");
		exit(0);
	}
	//发送文件
	send(sock,filename,sizeof(filename), 0);

	char buf[1024] = {0};
	int valread = read(sock, buf, sizeof(buf));
	while(valread > 0)
	{
		fwrite(buf, 1, valread, fp);

	}
	fclose(fp);

	printf("文件发送成功!\n");
	close(sock);
	return 0;




}
